package examples.webstore.domainobject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import edu.scsu.csci.annotation.validator.ADoubleValidator;
import edu.scsu.csci.annotation.validator.AStringValidator;
import edu.scsu.csci.controller.WebUtils;
import examples.webstore.Database;


/**
 * <p>	The product domain object.
 * 
 * @author Maqbool Khan
 * @since jdk 1.5
 * @see
 */
public class Product {
	
	/**
	 * <p>	product name
	 */
	@AStringValidator(isNullAllowed = false, maxLength = 40, minLength = 3, 
				errorMessage = "The product name has to be in between 3 and 40")
	private String name;
	
	/**
	 * <p>	product description
	 */
	private String productDescription;
	
	/**
	 * <p>	the selling price for the product
	 */
	@ADoubleValidator(max = 500000, min = 100,	
				errorMessage = "the product price has to be in between 100 and 500000")
	private double price;
	
	/**
	 * <p>	the person seeling the product 
	 */
	private User seller;
	
	/**
	 * <p>	date the product was added to the database
	 */
	private Date addedOn;
	
	
	public Product() {
		this.seller = (User) WebUtils.getSession().getAttribute("User");
		this.addedOn = new Date();
	}

	/**
	 * @param name
	 * @param sellerName
	 * @param productDescription
	 * @param price
	 * @param sellerAddress
	 */
	public Product(String name, String productDescription, double price, User seller) {

		// TODO Auto-generated constructor stub
		this.name = name;
		this.productDescription = productDescription;
		this.price = price;
		this.seller = seller;
		this.addedOn = new Date();
	}
	
	/**
	 * <p> returns <code>true</code> is the product is already available in the database. 
	 * @return
	 */
	public boolean isNotAvailable(){
			
		if(getProduct(this.name) != null)	{
			WebUtils.setErrorMessage("The product is already there in the database.");
			return false;
		}
		
		return true;
	}
	
	/**
	 * @param zipCode
	 * @return
	 */
	public static List<Product> displayList(long zipCode)	{
		
		if(zipCode <= 0){
			return Database.products;
		}
		
		List<Product> returnList = new ArrayList<Product>();
		
		for(int i=0, length = Database.products.size(); i < length; i++)	{
			if(Database.products.get(i).getZipCode() == zipCode){
				returnList.add(Database.products.get(i));
			}
		}
		
		return returnList;
	}
	
	
	/**
	 * @param zipCode
	 * @return
	 */
	public static Product getProduct(String name)	{
		
		for(int i=0, length = Database.products.size(); i < length; i++)	{
			if(Database.products.get(i).getName().equalsIgnoreCase(name)){
				return Database.products.get(i);
			}
		}
		
		return null;
	
	}
	
	/**
	 * @param product
	 */
	public static void add(Product product)	{
		if(product != null){
			Database.products.add(product);
		}
	}
	
	/**
	 * 
	 */
	public static int getProductCountByUser(String userName, Date addedAfter)	{
		
		int count = 0;
		for(int i=0, length = Database.products.size(); i < length; i++)	{
			Product product = Database.products.get(i);
			
			if(product.getSellerName().equalsIgnoreCase(userName) && addedAfter.before(product.getAddedOn())){
				count++;
			}
		}
		
		return count !=0 ? count++ : count;
		
	}
	
	
	/**
	 * Returns name.
	 * @return Returns name.
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Returns price.
	 * @return Returns price.
	 */
	public double getPrice() {
		return price;
	}
	
	/**
	 * Returns productDescription.
	 * @return Returns productDescription.
	 */
	public String getProductDescription() {
		return productDescription;
	}
	
	/**
	 * Returns sellerAddress.
	 * @return Returns sellerAddress.
	 */
	public String getSellerAddress() {
		return this.seller.getAddress();
	}

	/**
	 * Returns sellerName.
	 * @return Returns sellerName.
	 */
	public String getSellerName() {
		return this.seller.getName();
	}
	
	/**
	 * Returns zipCode.
	 * @return Returns zipCode.
	 */
	public long getZipCode() {
		return this.seller.getZipCode();
	}
	
	/**
	 * Returns addedOn.
	 * @return Returns addedOn.
	 */
	public Date getAddedOn() {
		return addedOn;
	}
		
}
